package com.example.hp.storeapp.Activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hp.storeapp.Const;
import com.example.hp.storeapp.R;
import com.example.hp.storeapp.base.BaseActivity;
import com.example.hp.storeapp.bean.CookResult;
import com.example.hp.storeapp.bean.DetailResult;
import com.example.hp.storeapp.bean.IsFavorResult;
import com.example.hp.storeapp.bean.NoFavorSResult;
import com.example.hp.storeapp.interf.RetrofitService;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class CookDetailActivity extends BaseActivity {
   private int foodId;
    private  DetailResult  detailResult;
    private TextView phonenumView;
    protected Context context;
    private TextView priceView;
    private TextView introView;
    private Button callButton;
    private TextView cookNameView;
    //private CookResult cook;

    public ImageView cookImageView;
    private Button cookButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cook_detail);
        foodId = getIntent().getIntExtra("food_id",-1);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<DetailResult> call = retrofitService.getFoodById(String.valueOf(foodId));

        call.enqueue(new Callback<DetailResult>() {
            @Override
            public void onResponse(Call<DetailResult> call, Response<DetailResult> response) {
                cookNameView.setText(response.body().getFoodname());
                priceView.setText(String.valueOf(response.body().getPrice()));
                introView.setText(response.body().getIntro());
                Picasso.with(context)
                        .load(response.body().getPic())
                        .into(cookImageView);
                getCollectionState(userId,String.valueOf(response.body().getFood_id()));
            }

            @Override
            public void onFailure(Call<DetailResult> call, Throwable t) {

            }
        });






        cookImageView = (ImageView) findViewById(R.id.imageview_detailcook);
        priceView = (TextView) findViewById(R.id.textView_food_price);
        introView = (TextView) findViewById(R.id.textView_intro);
        cookNameView = (TextView) findViewById(R.id.textView_food_name);
        phonenumView = (TextView) findViewById(R.id.textView_phonenum);
        phonenumView.setText(BaseActivity.phonenum);//显示电话号码

        cookButton = (Button) findViewById(R.id.cook_button);
        cookButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //点击收藏按钮，改变收藏状态

                doFavor(userId,String.valueOf(foodId));





            }
        });

        callButton = (Button) findViewById(R.id.button_call);
        callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));
                if (ActivityCompat.checkSelfPermission(CookDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });



    }

    private void getCollectionState(String usrId,String shop_food_id){
        //定义Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取Retrofit接口
       // final Button cookButton=new Button(context);
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<IsFavorResult> call = retrofitService.isCollected(usrId,shop_food_id,"1");
        call.enqueue(new Callback<IsFavorResult>() {
            @Override
            public void onResponse(Call<IsFavorResult> call, Response<IsFavorResult> response) {
                //获取店铺收藏状态

                if (response.body().getCollected().equals("1")){
                    //已收藏
                    cookButton.setText("已收藏");


                }
                else{
                    //未收藏

                }

            }

            @Override
            public void onFailure(Call<IsFavorResult> call, Throwable t) {

            }
        });
    }



    private void doFavor(String userId,String food_id){
        //定义Retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取retrofit接口
        RetrofitService retrofitService =
                retrofit.create(RetrofitService.class);

        Call<NoFavorSResult> call = retrofitService.userCollectFood(userId,String.valueOf(food_id));
        call.enqueue(new Callback<NoFavorSResult>() {
            @Override
            public void onResponse(Call<NoFavorSResult> call, Response<NoFavorSResult> response) {
                if (response.body().getSuccess().equals("1")){
                    //操作成功
                    Toast.makeText(CookDetailActivity.this,"收藏菜谱成功",Toast.LENGTH_SHORT).show();
                }else {

                }
            }

            @Override
            public void onFailure(Call<NoFavorSResult> call, Throwable t) {

            }
        });

    }






}
